import { QuestDefaultApi } from '@/library/util/api';
import { defaultErrorHandler } from '@/library/util/error-handler';
import { defineStore } from 'pinia';
import ExamAdapter from './adpater/ExamAdapter';
import { QuestsType } from './types';
interface DesignSliderState {
  // demo: string;

  questList: QuestsType[];
}
export const useDesignSliderStore = defineStore({
  id: 'DesignSliderStore',
  state: (): DesignSliderState => ({
    // demo: '',
    questList: [],
  }),

  getters: {
    /**
     * 获取题型
     * @returns
     */
    getQueryList(): QuestsType[] {
      const result = this.questList;
      return result;
    },
  },
  actions: {
    /**
     * 获取试题类型
     */
    async getExamType(typeId: string) {
      try {
        // console.log('getExamType', typeId);
        const result = await QuestDefaultApi.typeTypeListGet({
          typeId: typeId,
        });

        if (result) {
          this.questList = new ExamAdapter().parse(result);

          // 去除多项填空
          this.questList = this.questList.map((item) => ({
            id: item.id,
            types: item.types.filter((type) => type.id !== 7),
            queType: item.queType,
            typeName: item.typeName,
          }));
          // console.log(result2, this.questList);
        }
      } catch (error) {
        defaultErrorHandler(error);
      }
    },
  },
});
